#include <bits/stdc++.h>
using namespace std;
int T=0;
struct q{
    int pos;
    int cur;
};
struct match{
    int x,y,num;
};
bool cmp(match ma, match mb)
{
    if(ma.num!=mb.num)
        return ma.num>mb.num;
    return ma.x>mb.x;
}
void solve()
{
    int n=0,ans=0;
    int a[200005]={0},col[200005]={0};
    map<int,q> st;
    vector<match> arr;
    cin >> n;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i];
        ++st[a[i]].cur;
        int k=st[a[i]].cur;
        if(k&1)
            st[a[i]].pos=i;
        else
            arr.push_back({st[a[i]].pos,i,a[i]});
    }
    sort(arr.begin(), arr.end(), cmp);
    for(int i=0; i<arr.size(); i++)
    {
        int adj[5];
        adj[1]=col[arr[i].x-1>=0? arr[i].x-1: arr[i].x+1];
        adj[2]=col[arr[i].x+1];
        adj[3]=col[arr[i].y-1];
        adj[4]=col[arr[i].y+1<n? arr[i].y+1: arr[i].y-1];
        if(adj[1]==adj[2] && adj[3]==adj[4])
        {
            ans+=arr[i].num;
            if(adj[1]==0) col[arr[i].x]=1;
            if(adj[1]==1) col[arr[i].x]=2;
            if(adj[1]==2) col[arr[i].x]=1;
            if(adj[4]==0) col[arr[i].y]=1;
            if(adj[4]==1) col[arr[i].y]=2;
            if(adj[4]==2) col[arr[i].y]=1;
        }
        else continue;
    }
    cout << ans << endl;
}
int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    cin >> T;
    while(T--)
        solve();
    return 0;
}
